<template>
	<view class="login-container">
		<!-- 登录Logo -->
		<view class="logo-container">
			<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
			<text class="app-name">商务出行</text>
		</view>

		<!-- 登录表单 -->
		<view class="login-form">
			<!-- 手机号输入 -->
			<view class="input-group">
				<text class="input-label">手机号</text>
				<input class="input" type="number" placeholder="请输入手机号" placeholder-class="placeholder"
					v-model="mobileNumber" maxlength="11" />
			</view>

			<!-- 密码输入 -->
			<view class="input-group">
				<text class="input-label">密码</text>
				<view class="password-input">
					<input class="input" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
						placeholder-class="placeholder" v-model="password" />
					<text class="eye-icon" @click="togglePassword">
						{{ showPassword ? '👁️' : '👁️‍🗨️' }}
					</text>
				</view>
			</view>

			<!-- 登录按钮 -->
			<button class="login-btn" @click="handleLogin" :disabled="!isFormValid">登录</button>

			<!-- 忘记密码和注册 -->
			<!-- <view class="login-options">
				<text class="option-text" @click="navigateToChangePassword">忘记密码？</text>
			</view> -->
		</view>

		<!-- 加载提示 -->
		<uni-load-more v-if="loading" :status="'loading'" :content-text="{ loadingText: '登录中...' }"></uni-load-more>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { login } from '@/api'

// 响应式数据
const mobileNumber = ref('')
const password = ref('')
const showPassword = ref(false)
const loading = ref(false)

// 计算属性：表单验证
const isFormValid = computed(() => {
	// 简单的手机号验证（11位数字）
	const mobileNumberRegex = /^1[3-9]\d{9}$/
	return mobileNumberRegex.test(mobileNumber.value) && password.value.length >= 3
})

// 切换密码显示状态
const togglePassword = () => {
	showPassword.value = !showPassword.value
}

// 处理登录
const handleLogin = async () => {
	if (!isFormValid.value) {
		uni.showToast({
			title: '请输入正确的手机号和密码',
			icon: 'none'
		})
		return
	}

	loading.value = true
	try {
		// 调用登录API
		const res = await login({ mobileNumber: mobileNumber.value, password: password.value })

		// 检查登录是否成功
		if (res && res.code === 200) {
			// 保存token到本地存储
			if (res.data && res.data.token) {
				uni.setStorageSync('token', res.data.token)
				uni.setStorageSync('data', res.data)
			}

			uni.showToast({
				title: '登录成功',
				icon: 'success'
			})
			// 登录成功后跳转到首页
			tabBarIndex(0)
		} else {
			uni.showToast({
				title: res?.msg || '登录失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('登录失败:', error)
		uni.showToast({
			title: '网络异常，请稍后重试',
			icon: 'none'
		})
	} finally {
		loading.value = false
	}
}

// 跳转到修改密码页面
const navigateToChangePassword = () => {
	uni.navigateTo({
		url: '/pages/changePassword/index'
	})
}

// 跳转到tabBar页面
const tabBarIndex = (index) => {
	uni.switchTab({
		url: '/pages/tabBar/index/index'
	})
}
</script>

<style scoped>
.login-container {
	padding: 60rpx 40rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	min-height: 100vh;
	body-bg-color: #f5f5f5;
}

.logo-container {
	margin-top: 100rpx;
	margin-bottom: 80rpx;
	text-align: center;
}

.logo {
	height: 100rpx;
	/* width: 160rpx; */
	margin: 0 auto;
	display: block;
	/* border-radius: 20rpx; */
}

.app-name {
	display: block;
	font-size: 40rpx;
	font-weight: bold;
	color: #333;
	margin-top: 10rpx;
}

.login-form {
	width: 100%;
	max-width: 600rpx;
}

.input-group {
	margin-bottom: 40rpx;
}

.input-label {
	display: block;
	font-size: 28rpx;
	color: #666;
	margin-bottom: 10rpx;
}

.input {
	width: 100%;
	height: 90rpx;
	border: 1rpx solid #ddd;
	border-radius: 10rpx;
	padding: 0 30rpx;
	font-size: 32rpx;
	background-color: #fff;
	box-sizing: border-box;
}

.placeholder {
	color: #999;
}

.password-input {
	position: relative;
	display: flex;
	align-items: center;
}

.password-input .input {
	padding-right: 90rpx;
}

.eye-icon {
	position: absolute;
	right: 30rpx;
	font-size: 40rpx;
	color: #999;
}

.login-btn {
	width: 100%;
	height: 90rpx;
	background-color: #007aff;
	color: #fff;
	font-size: 34rpx;
	font-weight: bold;
	border-radius: 10rpx;
	margin-top: 60rpx;
}

.login-btn[disabled] {
	background-color: #cccccc;
}

.login-options {
	display: flex;
	justify-content: flex-end;
	margin-top: 30rpx;
}

.option-text {
	font-size: 28rpx;
	color: #007aff;
}
</style>
